package com.tuling.mybaitis_spring;

import com.tuling.annotation.ZhouyuMapperScan;
import com.tuling.mybaitis_spring.mapper.UserMapper;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

public class MapperBedifinitionRegister implements ImportBeanDefinitionRegistrar {
	@Override
	public void registerBeanDefinitions(AnnotationMetadata annotationMetadata,
										BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {

		//获取注解
		Map<String, Object> annotationAttributes = annotationMetadata.getAnnotationAttributes(ZhouyuMapperScan.class.getName());
		String path = (String)annotationAttributes.get("value");
		System.out.println(path);
		// 实现包扫描
		ClassPathBeanDefinitionScanner scanner = new ZhouyuClassPathBeanDefinitionScanner(registry);
		scanner.addIncludeFilter(new TypeFilter() {
			@Override
			public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
				return true;
			}
		});
		scanner.scan(path);


		ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(annotationMetadata, registry, importBeanNameGenerator);
	}
}
